/**
 * Created by cjh1 on 2016/9/22.
 */
// 文字显示更多插件
;(function ($) {

    function MoreText(elem, option) {

        var that = this;

        var DEFAULTS = {
            maxLength: 0,
            maxHeight: 0,
            showText: '<i class="icon icon-down"></i> 展开',
            hideText: '<i class="icon icon-top"></i> 隐藏',
            handle: 0,
        };

        var opts = $.extend({}, DEFAULTS, option);

        var $elem = $(elem);

        $elem.addClass('more-text-content');
        //$elem.css('overflow', 'hidden');

        if (opts.maxHeight) {
            // 限制最大高度
            var height = $elem.height();
            if (height > opts.maxHeight) {
                $elem.data('initHeight', height);
                $elem.data('optHeight', opts.maxHeight);

                $elem.height(opts.maxHeight);
                $elem.addClass('more-flag');
                if (opts.handle) {
                    that.$handle = $(opts.handle);
                } else {
                    that.$handle = $('<a class="content-more" href="#">显示更多</a>');
                    $elem.append(that.$handle);
                }

                that.$handle.html(opts.showText);

                that.$handle.on('click', function (e) {
                    e.preventDefault();

                    if ($elem.hasClass('more-flag')) {
                        console.log('这里',$elem.data('initHeight'));
                        $elem.removeClass('more-flag');
                        that.$handle.html(opts.hideText);
                        $elem.height($elem.data('initHeight'));
                    } else {
                        $elem.addClass('more-flag');
                        that.$handle.html(opts.showText);
                        $elem.height($elem.data('optHeight'));
                    }
                });
            }
        } else {
            // 限制最大长度
            var text = $elem.text();
            var length = text.length;
            console.log(length);
            if (length > opts.maxLength) {
                console.log(1)
                var html = text.substring(0, opts.maxLength);

                html += '<span class="text-more-text">' + text.substring(opts.maxLength) + '</span>';
                html += '<a class="text-more" href="#">显示更多</a>';
                $elem.html(html);
            }
        }

        $('.text-more').on('click', function (e) {
            e.preventDefault();

            var $this = $(this);

            var $moreText = $this.parent().find('.text-more-text');
            if ($moreText.is(":hidden")) {
                $this.text('隐藏')
            } else {
                $this.text('显示更多...')
            }
            $moreText.toggle();


        });

    }

    $.fn.moreText = function (option) {

        return (this).each(function () {
            new MoreText(this, option);
        });

    };
})(jQuery);

$(document).ready(function () {

    // 公司介绍加载更多
    $('#com-desc').moreText({
        maxHeight: 98,
        handle: '#more-desc'
    })

    // 产品轮播


    // 快速输入
    $('#quick-list').on('click', 'a', function (e) {
        e.preventDefault();

        $('#like-text').val($(this).text());
    });

    // 用户喜欢公司
    $('#like').on('click', function (e) {
        e.preventDefault();

        var $this = $(this);
        var likeId = $this.parents('tr').data('id');

        if (page.userType !== '' && page.userType !== '1') {
            eui.msg('只有学生才可以LIKE企业');
            return;
        }

        if (page.userType !== '1') {
            $('#login-wrap').dialog();
            return;
        }


        /*if ($this.hasClass('disabled')) {
         eui.msg('不能重复LiKE');
         return;
         }*/
        if (page.userType === '1') { // 用户已经登陆
            $('#box-like-login').hide();
        } else {
            $('#quick-box').hide();
        }
        $('#like-dialog').dialog();
    });

    $('#like-btn').on('click', function () {
        var value = $('#like-text').val();
        if (!value) {
            eui.msg('请输入内容');
            return;
        }
        $.ajax({
            url: '/user/like',
            data: {
                id: page.companyId,
                like_msg: value
            },
            type: 'POST',
            dataType: 'json',
            success: function (obj) {
                if (obj.code === 0) {
                    $('#like-dialog').dialog('hide');

                    $('#like').addClass('disabled');
                    //$this.attr('disabled', true);
                    eui.msg('发送成功，请耐心等待');
                    //eui.alert('成功');
                } else {
                    eui.msg('LIKE失败，'+obj.msg);
                }

                //$('#like-' + likeId).remove();
            },
            error: function () {
                eui.msg('系统出错');
            }
        })
    });
    $('.box-like .cancel').on('click', function() {
        $('#like-dialog').dialog('hide');
    });
    // 点赞公司
    $('#parise').on('click', function (e) {
        e.preventDefault();

        var $this = $(this);

        if ($this.hasClass('disabled')) {
            eui.msg('您已经点过赞了');
            return;
        }
        $.ajax({
            url: '/user/parise',
            data: {
                aim_id: page.companyId,
                aim_type: 2 // 1赞学生2赞企业
            },
            type: 'POST',
            dataType: 'json',
            success: function (obj) {
                if (obj.code === 0) {
                    //eui.msg('操作成功');
                    var $num = $('#parise-num');
                    $num.text((parseInt($num.text()) + 1));
                    $num.parent().addClass('disabled');

                    /*$this.html('<i class="fa fa-thumbs-up"></i> ' + (parseInt($this.text()) + 1));
                     $this.attr('disabled', true);*/
                } else {
                    eui.alert('操作失败，' + obj.msg);
                }

                //$('#like-' + likeId).remove();
            },
            error: function () {
                eui.alert('系统出错');
            }
        })
    });


    // 相关企业
    companyAjax(1, false);

    function companyAjax(page, updatePage) {
        $.ajax({
            url: '/cpn/relev',
            data: {
                page: page,
                pageSize: 6
            },
            dataType: 'json',
            success: function (obj) {
                companyView(obj);
                if (updatePage) {
                    companyPage(obj);
                }
            },
            error: function () {
                eui.alert('系统出错');
            }
        });
    }

    function companyView(obj) {
        var list = obj.list;
        var $ul = $('#relative-company');
        var html = '';
        for (var i = 0; i < list.length; i++) {
            var user = list[i];
            html += '<li class="list-item">'
                + '<img class="item-logo" src="' + user.logo + '">'
                + '<div class="item-info">'
                + '<a class="item-name" href="/cpn/detail?id=' + user.id + '">' + user.name + '</a>'
                + '<div class="item-profession">' + user.area + '</div>'
                + '<div class="item-school">' + user.industry + '</div>'
                + '</div></li>';

        }
        $ul.empty();
        $ul[0].innerHTML = html;
    }

    function companyPage(obj) {
        $('#relative-company-page').page({
            page: parseInt(obj.page),
            pageSize: 2,
            totalPage: obj.maxPage,
            onPage: function (page) {
                companyAjax(page);
            }
        });
    }
});